package handle

import (
	"fmt"
	"io"

	utilx "protocx/entity"

	"github.com/gogf/gf/v2/net/ghttp"
)

type Req struct {
}

func (p Req) InitRouter(group *ghttp.RouterGroup) {
	r := group.Group("/req")
	r.GET("query", p.query())
	r.POST("body", p.body())
	r.POST("json", p.json())
	r.POST("xml", p.xml())
	r.POST("array", p.array())
	r.POST("raw", p.raw())
	r.POST("form-data", p.formdata())
	r.POST("form-ulencoded", p.formulencoded())
}
func (p Req) query() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		name := r.Get("name", "unknown").String()
		age := r.GetQuery("age").String()
		r.Response.WriteJson(utilx.RespHandle(200, "success", name+age))
	}
}

func (p Req) body() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		fmt.Println(string(r.GetBody()))
	}
}

func (p Req) json() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		json, err := r.GetJson()
		if err != nil {
			utilx.RespHandle(400, "error", err)
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", json.Interface()))
	}
}

func (p Req) BodyJsonParse() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}

func (p Req) raw() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		all, err := io.ReadAll(r.Body)
		if err != nil {
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", string(all)))
	}
}
func (p Req) formdata() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		for k, v := range r.MultipartForm.File {
			fmt.Println(k, v)
		}
		for k, v := range r.MultipartForm.Value {
			fmt.Println(k, v)
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", r.Form))
	}
}
func (p Req) formulencoded() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		fmt.Println(r.PostForm)
		fmt.Println(r.MultipartForm)
		fmt.Println(r.GetMultipartForm())

	}
}

func (p Req) binary() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}
func (p Req) object() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}
func (p Req) xml() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}
func (p Req) array() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}
